package my.edu.model.information.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import my.edu.common.validate.groups.Insert;
import my.edu.common.domain.Response;
import my.edu.model.information.entity.dto.AddressSaveDTO;
import my.edu.model.information.service.IAddressService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/info/address")
@RestController
@Tag(name = "地址接口")
@Validated
public class AddressController {
    @Autowired
    private IAddressService addressService;

    @PutMapping
    @RequiresPermissions("address:save")
    @Operation(summary = "新增地址")
    public Response save(@Validated(value = Insert.class) @RequestBody AddressSaveDTO addressSaveDTO) {
        addressService.savenew(addressSaveDTO);
        return Response.success();
    }

    @GetMapping
    @RequiresPermissions("address:get")
    @Operation(summary = "获取地址列表")
    public Response getAddress() {
        return Response.success(addressService.list());
    }

    @GetMapping("/{id}")
    @RequiresPermissions("address:get")
    @Operation(summary = "根据编号获取地址信息")
    public Response getAddressByOn(@PathVariable @NotBlank(message = "id不能为空") String id) {
        return Response.success(addressService.getById(id));
    }

    @PostMapping("/update")
    @RequiresPermissions("address:update")
    @Operation(summary = "更新地址信息")
    public Response update(@Valid @RequestBody  AddressSaveDTO addressSaveDTO) {
        addressService.updateByDTO(addressSaveDTO);
        return Response.success();
    }

    @DeleteMapping
    @RequiresPermissions("address:del")
    @Operation(summary = "删除地址")
    public Response delete(@NotBlank(message = "id不能为空")Long id) {
        addressService.removeById(id);
        return Response.success();
    }

}
